package battleship;

public class Computer 
{
   boolean placeShip(BattleshipBoard board, int shipSize)
   {
      int boardHeight = board.getHeight();
      int boardWidth  = board.getWidth();
      int direction;
              
      if ( shipSize > boardHeight || shipSize > boardWidth ) 
          return false;
      
      if ( Math.random() > .5 )
          direction = BattleshipBoard.ShipHoriz;
      else
          direction = BattleshipBoard.ShipVert;
      
      int x, y;
      
      if ( direction == BattleshipBoard.ShipHoriz )
      {
         x = (int)(Math.random() * (boardWidth-shipSize+1));
         y = (int)(Math.random() * boardHeight);
      }
      else
      {
         x = (int)(Math.random() * boardWidth);
         y = (int)(Math.random() * (boardHeight-shipSize+1));
      }
     
      //
      // Place on the board.
      return board.placeShip(x, y, direction, shipSize);
   }
}
